<template>
  <div class="global-header">
    <a-row :wrap="false">
      <!-- Logo和标题区域 -->
      <a-col flex="auto" class="logo-section">
        <RouterLink to="/" class="logo-link">
          <div class="title-bar">
            <div class="logo-container">
              <img class="logo" src="../assets/logo.png" alt="WGrape-OJ Logo" />
            </div>
            <div class="title-container">
              <h1 class="title">WGrape-AI-Code</h1>
              <span class="subtitle">AI智能代码生成器系统</span>
            </div>
          </div>
        </RouterLink>
      </a-col>

           <!--  // 菜单 -->
        <a-col flex="auto">
            <div id="globalHeader">
                <a-menu v-model:selectedKeys="current" mode="horizontal" :items="items" @click="doMenuClick"/>
            </div>
        </a-col>

            <!-- // 登录 -->
        <a-col flex="120px">
          <div class="user-login-status">
            <div v-if="loginUserStore.loginUser.id">
              <a-dropdown>
                <ASpace>
                  <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                  {{ loginUserStore.loginUser.userName ?? '无名' }}
                </ASpace>
                <template #overlay>



                  <a-menu>
                    <a-menu-item>
                      <router-link to="/user/edit">
                        <SolutionOutlined />
                        个人信息
                      </router-link>
                    </a-menu-item>
                    <a-menu-item @click="doLogout">
                      <LogoutOutlined />
                      退出登录
                    </a-menu-item>
                  </a-menu>
                </template>
              </a-dropdown>
            </div>
            <div v-else>
              <a-button type="primary" href="/user/login">登录</a-button>
            </div>
          </div>
        </a-col>
    </a-row>
    </div>

</template>












<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import {
  FileTextOutlined, GithubOutlined,
  HomeOutlined,
  LogoutOutlined, MenuOutlined, PlusOutlined, SettingOutlined,
  SolutionOutlined, TeamOutlined
} from '@ant-design/icons-vue'
import { type MenuProps, message } from 'ant-design-vue'





const current = ref<string[]>(['home'])
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '首页',
    title: '首页',
  },
  {
    key: '/admin/userManage',
    icon: () => h(TeamOutlined),
    label: '用户管理',
    title: '用户管理',
    requireLogin: true,
  },
  {
    key: '/admin/appManage',
    label: '应用管理',
    title: '应用管理',
  },
  {
    key: '/admin/chatManage',
    label: '对话记录管理',
    title: '对话记录管理',
  },

  {
    key: 'others',
    label: h(
      'a',
      {
        href: 'https://gitee.com/zheng-fengnan/online-picture-vue',
        target: '_blank',

      },
      'Git源码',
    ),
    title: 'Git源码',
    icon: () => h( GithubOutlined),
  },
]
// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu.requireLogin && !loginUserStore.loginUser.id) {
      return false
    }
    if (menu.key.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== "admin") {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))
import { useRouter } from "vue-router";
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogout } from '@/api/userController.ts'


const router = useRouter();
// 路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  });
};

// 监听路由变化，更新当前选中菜单
router.afterEach((to, from, next) => {
  current.value = [to.path];
});

const loginUserStore = useLoginUserStore()
// loginUserStore.fetchLoginUser()
// 用户注销
const doLogout = async () => {
  const res = await userLogout()
  console.log(res)
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}

</script>














<style scoped>
.global-header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  border-bottom: 1px solid #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 1000;
  transition: all 0.3s ease;
}
/* Logo区域样式 */
.logo-section {
  min-width: 200px;
  max-width: 280px;
}

.logo-link {
  text-decoration: none;
  color: inherit;
  display: block;
  transition: all 0.3s ease;
}

.logo-link:hover {
  transform: translateY(-1px);
}

.title-bar {
  display: flex;
  align-items: center;
  height: 100%;
}

.logo-container {
  position: relative;
  margin-right: 12px;
}

.logo {
  height: 40px;
  width: 40px;
  border-radius: 8px;
  object-fit: cover;
  transition: all 0.3s ease;
}

.logo:hover {
  transform: scale(1.05);
}

.title-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.title {
  color: #1890ff;
  font-size: 20px;
  font-weight: 700;
  margin: 0;
  line-height: 1.2;
  background: linear-gradient(135deg, #1890ff, #722ed1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: #8c8c8c;
  font-size: 12px;
  line-height: 1;
  margin-top: 2px;
}

</style>
